// 配置 axios
import axios from 'axios'

axios.defaults.baseURL = 'http://127.0.0.1:8888/api/private/v1/'

/**
 * 请求拦截器 请求头添加 Authorization 验证
 */
axios.interceptors.request.use((config) => {
  config.headers.Authorization = window.sessionStorage.getItem('token')
  return config
}, function (error) {
  return Promise.reject(error)
})

/**
 * 登陆接口
 * @param params
 * @returns {Promise<AxiosResponse<T>>}
 */
export const apiLogin = (params) => axios.post('login', params)

/**
 * 获取权限菜单
 * @returns {Promise<AxiosResponse<T>>}
 */
export const apiGetMenus = () => axios.get('menus')

/**
 * 获取用户列表
 * @param params
 * @returns {Promise<AxiosResponse<T>>}
 */
export const apiGetUsers = (params) => axios.get('users', { params })

/**
 * 更新用户状态
 * @param params
 * @returns {Promise<AxiosResponse<T>>}
 */
export const apiUpdateUsersState = (params) => axios.put(`users/${params.id}/state/${params.mg_state}`)

/**
 * 添加用户接口
 * @param params
 * @returns {Promise<AxiosResponse<T>>}
 */
export const apiAddUser = (params) => {
  return axios.post('users', params)
}

/**
 * 查询用户详细信息
 * @param id
 * @returns {Promise<AxiosResponse<T>>}
 */
export const apiQueryUserInfo = (id) => {
  return axios.get(`users/${id}`)
}

/**
 * 升级用户信息
 * @param params
 * @returns {Promise<AxiosResponse<T>>}
 */
export const apiUpdateUserInfo = (params) => {
  return axios.put(`users/${params.id}`, {
    email: params.email,
    mobile: params.mobile
  })
}

/**
 * 删除用户
 * @param id
 * @returns {Promise<AxiosResponse<T>>}
 */
export const apiDeleteUser = (id) => {
  return axios.delete(`users/${id}`)
}

/**
 * 获取所有权限列表
 * @param type
 * @returns {Promise<AxiosResponse<T>>}
 */
export const apiQueryRights = (type = 'list') => {
  return axios.get(`rights/${type}`)
}

/**
 *  角色列表
 * @returns {Promise<AxiosResponse<T>>}
 */
export const apiGetRoles = () => {
  return axios.get('roles')
}

/**
 * 添加角色
 * @param params
 * @returns {Promise<AxiosResponse<T>>}
 */
export const apiSetRole = (params) => {
  return axios.post('roles', params)
}

/**
 * 编辑角色接口
 * @param id
 * @param params
 * @returns {Promise<AxiosResponse<T>>}
 */
export const apiEditRole = (id, params) => {
  return axios.put(`roles/${id}`, params)
}

/**
 * 获取角色
 * @param id
 * @returns {Promise<AxiosResponse<T>>}
 */
export const apiGetRole = (id) => {
  return axios.get(`roles/${id}`)
}

/**
 * 删除角色
 * @param id
 * @returns {Promise<AxiosResponse<T>>}
 */
export const apiDeleteRole = (id) => {
  return axios.delete(`roles/${id}`)
}

/**
 * 删除用户权限
 * @param params
 * @returns {Promise<AxiosResponse<T>>}
 */
export const apiDeleteRolePower = (params) => {
  return axios.delete(`roles/${params.roleId}/rights/${params.rightId}`)
}

/**
 * 角色授权
 * @param roleId
 * @param params
 * @returns {Promise<AxiosResponse<T>>}
 */
export const apiSetRoles = (roleId, params) => {
  return axios.post(`roles/${roleId}/rights`, { rids: params })
}

/**
 * 分配用户角色
 * @param id
 * @param rid
 * @returns {Promise<AxiosResponse<T>>}
 */
export const apiAllotRole = (id, rid) => {
  return axios.put(`users/${id}/role`, {
    rid: rid
  })
}

/**
 * 获取分类数据
 * @param params
 * @returns {Promise<AxiosResponse<T>>}
 */
export const apiGetCategories = (params = {}) => {
  return axios.get('categories', { params })
}

/**
 * 添加分类
 * @param params
 * @returns {Promise<AxiosResponse<T>>}
 */
export const apiAddCategory = (params) => {
  return axios.post('categories', params)
}

/**
 * 删除分类
 * @param id
 * @returns {Promise<AxiosResponse<T>>}
 */
export const apiDeleteCategory = (id) => {
  return axios.delete(`categories/${id}`)
}

/**
 * 编辑分类
 * @param params
 * @returns {Promise<AxiosResponse<T>>}
 */
export const apiEditCategory = (params) => {
  return axios.put(`categories/${params.id}`, { cat_name: params.cat_name })
}

/**
 * 获取单条分类数据
 * @param id
 * @returns {Promise<AxiosResponse<T>>}
 */
export const apiGetCategory = (id) => {
  return axios.get(`categories/${id}`)
}

/**
 * 获取参数列表
 * @param id
 * @param params
 * @returns {Promise<AxiosResponse<T>>}
 */
export const apiGetCateAttributes = (id, params) => {
  return axios.get(`categories/${id}/attributes`, { params })
}

/**
 * 添加 参数/属性
 * @param id
 * @param params
 * @returns {Promise<AxiosResponse<T>>}
 */
export const apiAddCateAttributes = (id, params) => {
  return axios.post(`categories/${id}/attributes`, params)
}

/**
 * 修改 参数/属性 标签
 * @param id
 * @param attrId
 * @param params
 * @returns {Promise<AxiosResponse<T>>}
 */
export const apiEditCateAttributes = (id, attrId, params) => {
  return axios.put(`categories/${id}/attributes/${attrId}`, params)
}

/**
 *
 * 删除参数
 * @param id
 * @param attrid
 * @returns {Promise<AxiosResponse<T>>}
 */
export const apiDeleteCateAttributes = (id, attrid) => {
  return axios.delete(`categories/${id}/attributes/${attrid}`)
}

// -----------------------------------------------------------

/**
 * 获取商品列表
 * @param params
 * @returns {Promise<AxiosResponse<T>>}
 */
export const apiGetGoodsList = (params) => {
  return axios.get(`goods`, { params })
}

/**
 * 添加商品
 * @param params
 * @returns {Promise<AxiosResponse<T>>}
 */
export const apiAddGoods = (params) => {
  return axios.post('goods', params)
}

// -----------------------------------------------------------

/**
 * 获取订单列表信息
 * @param params
 * @returns {Promise<AxiosResponse<T>>}
 */
export const apiGetOrdersList = (params) => {
  return axios.get('orders', { params })
}

/**
 * 获取物流信息
 * @param id
 * @returns {Promise<AxiosResponse<T>>}
 */
export const apiGetLogistic = (id) => {
  return axios.get(`/kuaidi/${id}`)
}

// ----------------------------------------------------------

/**
 * 获取基于时间统计的折线图
 * @returns {Promise<AxiosResponse<T>>}
 */
export const apiGetReports = () => {
  return axios.get(`reports/type/1`)
}
